package com.edais.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.edais.service.ClickCountService;
import com.edais.service.UserService;
import com.edais.util.CommonUtil;
import com.edais.util.JsonUtil;



public class FuncClickInterceptor extends HandlerInterceptorAdapter  {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ClickCountService clickCountService;

	/**
	 * 功能点限制
	 * 
	 * @param req
	 * @param res
	 * @param handler
	 * @return boolean
	 */
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
			Object handler) throws Exception {
		
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(req);

		String userId = paramsMap.get("userId");// 用户id
		String mobile = paramsMap.get("mobile"); // 手机号
		String phoneNum = paramsMap.get("phoneNum"); // 手机号
		String funcId = paramsMap.get("funcId"); // 功能id
		String interfaceName=getInterfaceName(req); // 接口名称
		String channel = paramsMap.get("from"); // 渠道
		
		if (StringUtils.isBlank(funcId)) {
			if (StringUtils.isBlank(interfaceName)) {
				return true;
			}
		}
		Map<String,String> userMap = new HashMap<String,String>();
		userMap.put("interfaceName", interfaceName);
		userMap.put("funcId", funcId);
		if(!clickCountService.checkClickCount(userMap))
			return true;
		
		if (mobile == null && phoneNum != null)
			mobile = phoneNum;
		try {
			// 手机号
			if (StringUtils.isBlank(mobile)) {
				if (StringUtils.isBlank(userId)) {
					mobile = "18000000000";
				} else {
					mobile = (String) userService.getUserMobile(userId).get("mobile");
				}
			}
 
			if (StringUtils.isBlank(channel)) {
				channel = "LBWX";
			}

			userMap.put("channel", channel);
			userMap.put("mobile", mobile);

			// 处理返回结果
			String resultMsg = JsonUtil.getJsonStringFromMap(this.clickCountService.getClickCount(userMap));

			String clickRescode = "";
			if (resultMsg != null && !"".equals(resultMsg)) {
				JSONObject jsonObject = JSONObject.fromObject(resultMsg);
				clickRescode = jsonObject.getString("clickRescode");
			}
			// 判断返回
			if ("00004".equals(clickRescode)) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}


	/**
	 * 获取功能对应接口名
	 * 
	 * @param req
	 * @return result
	 */
	public String getInterfaceName(HttpServletRequest req)
	{
		String result="";
		String url=req.getRequestURL().toString();
		String[] temp=url.split("/");
		int length=temp.length;
		if (length<2)
			return result;
		result="/"+temp[length-2]+"/"+temp[length-1];
		return result;
	}
	
}
